package com.zzx.utils;

import com.intellij.openapi.diagnostic.Logger;
import kong.unirest.Callback;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.UnirestException;


import javax.swing.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;


public class HttpUtil {
    private static final Logger log = Logger.getInstance(HttpUtil.class);
    public static String put(String url, String username, String password, File file, JTextArea textArea, JProgressBar progressBar, AtomicLong atomicLong, AtomicBoolean flag) {
        try {
            log.info("url:"+url+",username:"+username+",file:"+file.getAbsoluteFile());
            InputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            inputStream.read(bytes);
            Future<HttpResponse<String>> response =  Unirest.put(url)
                    .basicAuth(username,password)
                    .header("Content-Type", "application/octet-stream")
                    .body(bytes)
                    .asStringAsync(new Callback<String>() {
                        @Override
                        public void completed(HttpResponse<String> httpResponse) {
                            if(httpResponse.getStatus()==400){
                                textArea.append("已经完成:"+file.getAbsolutePath()+",code:"+httpResponse.getStatus()+"\n");
                            }else if(httpResponse.getStatus()==201){
                                textArea.append("上传完成:"+file.getAbsolutePath()+",code:"+httpResponse.getStatus()+"\n");
                            }else {
                                textArea.append("上传失败:"+file.getAbsolutePath()+",code:"+httpResponse.getStatus()+",详细信息:"+httpResponse.getBody()+"\n");
                            }
                            long num = atomicLong.incrementAndGet();

                            progressBar.setValue((int) num);
                            if(num>=progressBar.getMaximum()){
                                textArea.append("上传完成\n");
                                progressBar.setVisible(false);
                                flag.set(false);
                            }
                        }
                        @Override
                        public void cancelled() {
                            textArea.append("取消上传:"+file.getAbsolutePath()+"\n");
                            long num = atomicLong.incrementAndGet();
                            progressBar.setValue((int) num);
                            if(num>=progressBar.getMaximum()){
                                textArea.append("上传完成\n");
                                progressBar.setVisible(false);
                                flag.set(false);
                            }
                        }
                        @Override
                        public void failed(UnirestException e) {
                            textArea.append("上传失败:"+file.getAbsolutePath()+",详细信息:"+e.getMessage()+"\n");
                            long num = atomicLong.incrementAndGet();
                            progressBar.setValue((int) num);
                            if(num>=progressBar.getMaximum()){
                                textArea.append("上传完成\n");
                                progressBar.setVisible(false);
                                flag.set(false);
                            }
                        }
                    });
        }catch (Exception e){
            textArea.append("上传失败:"+file.getAbsolutePath()+",详细信息:"+e.getMessage()+"\n");
            long num = atomicLong.incrementAndGet();
            progressBar.setValue((int) num);
            if(num>=progressBar.getMaximum()){
                textArea.append("上传完成\n");
                progressBar.setVisible(false);
                flag.set(false);
            }
        }
        return "";
    }
}
